
//  TRWeather.m
//  Weather-UI-V1
//
//  Created by tarena on 15-6-24.
//  Copyright (c) 2015年 tarena. All rights reserved.
//

#import "TRWeather.h"

//天气图标url前缀
static NSString * const iconURLString = @"http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_";

@implementation TRWeather

+ (id)weatherWithCurrentDic:(NSDictionary *)dic {
    
    return [[self alloc] initWithCurrentDic:dic];
}

- (id)initWithCurrentDic:(NSDictionary *)dic {
    if (self = [super init]) {
        //当前的温度
        self.tempC = [dic[@"data"][@"current_condition"][0][@"temp_C"] floatValue];
        //天气描述
        self.weatherDesc = dic[@"data"][@"current_condition"][0][@"weatherDesc"][0][@"value"];
        NSURL *iconUrl = dic[@"data"][@"current_condition"][0][@"weatherIconUrl"][0][@"value"];
        //图标
        self.iconName = [self imageMap][iconUrl];
        //城市名字
        self.cityName = dic[@"data"][@"request"][0][@"query"];
        //最高温度
        self.maxTemp = [dic[@"data"][@"weather"][0][@"maxtempC"] floatValue];
        
        //最低温度
        self.minTemp = [dic[@"data"][@"weather"][0][@"mintempC"] floatValue];
    }
    
    return self;
    
}

+ (id)weatherWithHourlyDic:(NSDictionary *)dic {
    return [[self alloc] initWithHourlyDic:dic];
}

- (id)initWithHourlyDic:(NSDictionary *)dic {
    if (self == [super init]) {
        
        //tempC
        self.tempC = [dic[@"tempC"] floatValue];
        
        //time
        self.hourly = [dic[@"time"] floatValue] / 100;
        
        //weatherIconUrl
        NSURL *url = dic[@"weatherIconUrl"][0][@"value"];
        self.iconName = [self imageMap][url];
        

    }
    
    return self;
}

+ (id)weatherWithDailyDic:(NSDictionary *)dic {
    return [[self alloc] initWithDailyDic:dic];
}

- (id)initWithDailyDic: (NSDictionary *)dic {
    
    if (self == [super init]) {
        self.date = dic[@"date"];
        self.minTemp = [dic[@"mintempC"] floatValue];
        self.maxTemp = [dic[@"maxtempC"] floatValue];

        //weatherIconUrl
        NSURL *url = dic[@"hourly"][0][@"weatherIconUrl"][0][@"value"];
        self.iconName = [self imageMap][url];
    }
    
    return self;
}


#pragma mark --- 图标映射
- (NSDictionary *)imageMap{
    
    return @{
             [NSString stringWithFormat:@"%@0001_sunny.png",iconURLString]:@"weather-clear.png",
             [NSString stringWithFormat:@"%@0003_white_cloud.png",iconURLString]:@"weather-few.png",
             [NSString stringWithFormat:@"%@0004_black_low_cloud.png",iconURLString]:@"weather-scattered.png",
             [NSString stringWithFormat:@"%@0004_black_low_cloud.png",iconURLString]:@"weather-broken",
             [NSString stringWithFormat:@"%@0009_light_rain_showers.png",iconURLString]:@"weather-shower",
             [NSString stringWithFormat:@"%@0025_light_rain_showers_night.png",iconURLString]:@"weather-rain",
             @"11d":@"weather-tstorm",
             @"13d":@"weather-snow",
             [NSString stringWithFormat:@"%@0006_mist.png",iconURLString]
             :@"weather-mist",
             [NSString stringWithFormat:@"%@0008_clear_sky_night.png",iconURLString]:@"weather-moon",
             @"02n":@"weather-few-night",
             @"03n":@"weather-few-night",
             [NSString stringWithFormat:@"%@0002_sunny_intervals.png",iconURLString]:@"weather-broken",
             @"09n":@"weather-shoer",
             [NSString stringWithFormat:@"%@0025_light_rain_showers_night.png",iconURLString]:@"weather-rain-night",
             @"11n":@"weather-tstorm",
             @"13n":@"weather-snow",
             @"50n":@"weather-mist"
             };
}







@end
